๐Ÿงฉ Part 1: Wrapper Classes in Java

๐Ÿ”ท What is a Wrapper Class?

A Wrapper class is a class whose object wraps a primitive data type. Java provides these classes in the java.lang package to convert primitive types into objects.

Java Wrapper Classes Hierarchy Primitive vs Wrapper Classes Table

Key Difference: Primitives are not objects, stored on stack, faster; Wrappers are objects, stored on heap, can be null, have methods.

๐Ÿ” Why Wrapper Classes are Needed?

Key Reasons:

  • Collections API: Like ArrayList, HashMap can only store objects (not primitives)
  • Utilities: Wrapper classes have methods like parseInt(), compare(), valueOf()
  • Autoboxing/Unboxing: Java automatically converts between primitives and wrappers
  • Can be null: Useful when working with databases where a value may be unknown
  • Generic Support: Generics in Java (like <T>) do not support primitives, only objects

๐Ÿ” Autoboxing and Unboxing

Autoboxing = primitive โžก๏ธ object (like putting primitive in a box)

Unboxing = object โžก๏ธ primitive (opening the box)

public class AutoBoxingExample {
    public static void main(String[] args) {
        int a = 10;

        // Autoboxing
        Integer obj = a;

        // Unboxing
        int b = obj;

        System.out.println("Object: " + obj);
        System.out.println("Primitive: " + b);
    }
}

Note: Introduced in Java 5 for seamless conversion. But beware of NullPointerException during unboxing if wrapper is null.

๐Ÿ’ผ Real-Life Example

Let's say you want to store user ages in a list:

ArrayList<Integer> ages = new ArrayList<>();
ages.add(25); // Autoboxing happens here: int โž Integer
ages.add(30);

int sum = ages.get(0) + ages.get(1); // Unboxing: Integer โž int
System.out.println("Sum of ages: " + sum);

Analogy: Primitives are like raw ingredients; wrappers are packaged versions for storage in a fridge (collections).

๐Ÿ› ๏ธ Useful Wrapper Class Methods

public class WrapperMethods {
    public static void main(String[] args) {
        String number = "100";

        int value = Integer.parseInt(number); // Convert string to int
        System.out.println("Parsed value: " + value);

        String text = Integer.toString(1234); // Convert int to string
        System.out.println("String value: " + text);

        boolean result = Boolean.parseBoolean("true");
        System.out.println("Boolean value: " + result);

        char ch = 'A';
        System.out.println("Is Digit: " + Character.isDigit(ch));
        System.out.println("Is Uppercase: " + Character.isUpperCase(ch));
    }
}

Key Point: All wrappers are immutable โ€“ once created, value can't change.

๐Ÿค” Interactive Self-Quiz for Wrapper Classes

Test your understanding with these expandable questions. Click to reveal answers.

What is the wrapper for int?

Integer

What happens in autoboxing?

Primitive to wrapper object conversion automatically.

Can wrappers be null?

Yes, unlike primitives.

Why use parseInt?

To convert String to int.

๐Ÿ“Œ Interview Point of View (Wrapper Class)

๐Ÿ”น Question ๐Ÿ’ฌ Expected Answer
What is a wrapper class? A class that converts a primitive type into an object.
Why are wrapper classes needed? For collections, generics, null handling, utilities.
Difference between Integer and int? int is primitive, Integer is a class (object).
What is autoboxing/unboxing? Auto conversion between primitive and wrapper.
Can you store int in ArrayList? Not directly โ€” it uses Integer via autoboxing.
Are wrapper classes immutable? Yes, their values cannot be changed after creation.
What is the risk with unboxing? NullPointerException if wrapper is null.
Difference between valueOf() and parseInt()? valueOf() returns Integer, parseInt() returns int.

๐Ÿ“˜ Generics in Java

๐Ÿ”น What is Generics?

Generics allows writing code that works with different data types while providing type safety.

Introduced in: Java 5

๐Ÿ”น Why Use Generics?

  • โœ… Type safety
  • โœ… Reusability
  • โœ… Compile-time checking
  • โœ… No need for type casting

๐Ÿ”น Generic Class Example

class Box<T> {
    T value;
    void setValue(T val) { value = val; }
    T getValue() { return value; }
}
    

๐Ÿ”น Using Generic Class

Box<String> s = new Box<>();
s.setValue("Hi");
System.out.println(s.getValue());
    

๐Ÿ”น Generic Method

public <T> void print(T[] arr) {
    for (T t : arr)
        System.out.println(t);
}
    

๐Ÿ”น Bounded Type

class Demo<T extends Number> {
    void show(T n) {
        System.out.println(n.doubleValue());
    }
}
    

๐Ÿ”น Wildcards

  • <?> โ€“ unknown type
  • <? extends Number> โ€“ any subclass of Number
  • <? super Integer> โ€“ any superclass of Integer

Analogy: Wildcards are like jokers in cards โ€“ flexible but with rules.

โœ… Summary

  • Generic = Type-safe reusable code
  • Used in collections, utilities, containers
  • Makes code more readable and safer

๐Ÿค” Interactive Self-Quiz for Generics

Test your understanding with these expandable questions. Click to reveal answers.

What problem do generics solve?

Type safety in collections without casting.

What is ?

Upper bounded wildcard โ€“ subtypes of T.

Can generics use primitives?

No, only reference types; use wrappers for primitives.

๐Ÿ“Œ Interview Point of View (Generics)

๐Ÿ”น Question ๐Ÿ’ฌ Expected Answer
What are generics in Java? Parameterized types for type-safe code.
Benefits of generics? Type safety, no casting, compile-time checks.
What is erasure? Generics info removed at compile-time for backward compatibility.
Difference between List and List? List is raw wildcard, can't add; List can add Objects.
What are bounded generics? Restrict types, e.g., .
PECS rule? Producer Extends, Consumer Super for wildcards.
๐Ÿ’ก Part 2: Why Java is Not Fully Object-Oriented

โ“ What is a Fully Object-Oriented Language?

A fully object-oriented language is one where:

Examples: Smalltalk, Ruby, Scala
Why Java is not Fully OOP Infographic

โŒ Why Java is Not 100% Object-Oriented?

Reason Explanation
Primitive Types Java has int, float, char, etc., which are not objects. (For performance reasons)
Static Methods/Variables Methods like Math.sqrt() don't need objects.
Limited Operator Overloading Custom operator overloading is not allowed.
Top-Level Code Outside Objects Static context used before object creation (e.g., main method).

Why Primitives? Faster execution, less memory โ€“ primitives on stack, objects on heap.

๐Ÿ”„ Comparison: Java vs Pure OOP

๐ŸŸก Java (Hybrid)

  • Has primitive types
  • Static methods exist
  • Performance optimized
  • Easier to learn

๐ŸŸข Pure OOP (Smalltalk)

  • Everything is an object
  • All operations via messages
  • Consistent design
  • More complex

๐Ÿค” Interactive Self-Quiz for OOP Concepts

Test your understanding with these expandable questions. Click to reveal answers.

Why primitives in Java?

For better performance and memory efficiency.

Is static method OOP?

No, as it doesn't require an object instance.

Example of pure OOP language?

Smalltalk or Ruby.

๐Ÿ“Œ Interview Point of View (Object-Oriented)

๐Ÿ”น Question ๐Ÿ’ฌ Expected Answer
Is Java fully object-oriented? No, because it uses primitive types and static members.
Why does Java have primitives? For performance โ€“ primitives are faster and use less memory.
What makes Java object-oriented? Class-based, supports encapsulation, inheritance, polymorphism, and abstraction.
What breaks pure OOP in Java? int, boolean, static methods/fields, etc.
Can Java be considered OOP? Yes, it's OOP but not purely due to non-object elements.
Advantages of not being pure OOP? Better performance with primitives, easier static utilities.

๐Ÿงพ Final Summary

โœ… Wrapper Class Summary

โœ… Java Is Not Fully OOP Because:

๐Ÿง  Quick Revision for Interviews